<?php

include('../functions/db.php');
include('../functions/common.php');

$photo_id = mres(($_POST['photo_id']));
$action = $_REQUEST['action'];
$url = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1);

if ($action == 'img_params') {
    $comments = getPhotoComments($photo_id);
    $output = '';
    foreach ($comments as $comment) {
        $src = $comment['slika'] ? 'media/autori/' . $comment['username'] . '/slike_za_profil/' . $comment['slika'] : 'media/images/user_img.png';
//parse comment smiles
        $comment['komentar'] = parseCommentSmiles($comment['komentar']);
        $output .= '<li class="clearfix" id="comment-id-' . $comment['id'] . '">
                    <img class="fl_left" src="' . $src . '" width="40" height="40" />
                    <span><a class="comment_aut" href="?page=profil&clan_id=' . $comment['autor_id'] . '">' . $comment['username'] . '</a> </span>' . $comment['komentar'] . '
                    <div>' . $comment['datum_vreme_upisa'] . '</div>
                </li>';
    }

    $image = getImageInfo($photo_id);

    $response['comments'] = $output;
    $response['image'] = $image;


    echo json_encode($response);
}
 elseif ($action=='like') {
    incrementImagelikes($photo_id);
}
 elseif ($action=='view') {
    incrementImageViews($photo_id);
}
 elseif ($action=='comment') {
    $comment_content = mres(($_POST['comment_content']));
    $username = mres(( $_POST['username']));
    addImageComment($photo_id, $comment_content, $username);
    header('Location: ../?page=galerija_single&photo_id='. $photo_id);
}

function parseCommentSmiles($commentText) {
    $commentText = str_replace(':)', '<img src="templates/tattoo/smiles/smile.png" />', $commentText);
    $commentText = str_replace(';)', '<img src="templates/tattoo/smiles/wink.png" />', $commentText);
    $commentText = str_replace(':[', '<img src="templates/tattoo/smiles/mad.png" />', $commentText);
    $commentText = str_replace('B)', '<img src="templates/tattoo/smiles/cool.png" />', $commentText);
    $commentText = str_replace(':P', '<img src="templates/tattoo/smiles/plez.png" />', $commentText);
    $commentText = str_replace('BP', '<img src="templates/tattoo/smiles/excited.png" />', $commentText);
    $commentText = str_replace(':O', '<img src="templates/tattoo/smiles/rofl.png" />', $commentText);

    return $commentText;
}
?>
